﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using StevensReceipts.Domain;

namespace StevensReceipts.Web.Areas.Service.Controllers
{
    public class ChartofAccountAccountTypeController : ApiControllerBase
    {
        //GET api/chartofaccountaccounttype
        public IEnumerable<ChartofAccountAccountType> Get()
        {
            return DataSource.ChartofAccountAccountTypes.GetAll();
        }

        public ChartofAccountAccountType Get(int id)
        {
            var accountType = DataSource.ChartofAccountAccountTypes.GetSingle(type => type.Id == id);
            if(accountType == null)
                throw new HttpResponseException(HttpStatusCode.NotFound);
            return accountType;
        }

        //POST api/chartofaccountaccounttype
        public void Post(ChartofAccountAccountType accountType)
        {
            DataSource.ChartofAccountAccountTypes.Add(accountType);
            DataSource.Save();
        }

        //PUT api/chartofaccountaccounttype/5
        public void Put(int id, ChartofAccountAccountType accountType)
        {
            ChartofAccountAccountType type = DataSource.ChartofAccountAccountTypes.GetSingle(c => c.Id == id);
            if(type == null)
                throw new HttpResponseException(HttpStatusCode.NotFound);
            type.CategoryId = accountType.CategoryId;
            type.Name = accountType.Name;
            type.Modified = accountType.Modified;

            DataSource.Save();
        }
    }
}
